{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Python 02 控制语句"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "C++中我们经常需要进行条件控制与判断，我们常使用到if-else switch等控制语句"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未进行类型转换\n",
      "已经转换\n",
      "98.0 优秀\n"
     ]
    }
   ],
   "source": [
    "#Python中也有条件控制语句  以以下小demo为例子\n",
    "#在写条件控制语句的时候注意缩进问题\n",
    "score=input(\"Please Input Your English Score:\")\n",
    "tag=0\n",
    "while(tag!=1):\n",
    "    if(type(score)!=type(3) and type(score)!=type(3.0)):\n",
    "        print(\"未进行类型转换\")\n",
    "        score=float(score)\n",
    "        print(\"已经转换\")\n",
    "    elif 90<=score<=100:\n",
    "        tag=1\n",
    "        print(score,\"优秀\")\n",
    "    else:\n",
    "        print(\"懒得写了\")\n",
    "        tag=1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 并列条件\n",
    "逻辑语句中往往涉及到 或 和 且 关系\n",
    "Python中 或用 or\n",
    "         且用 and"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num_a= 4\n",
      "num_b= 1\n"
     ]
    }
   ],
   "source": [
    "#for循环\n",
    "#Python中的for遍历的时可迭代对象\n",
    "#看以下示例\n",
    "lst1=[\"a\",\"b\",\"a\",\"a\",\"a\"]\n",
    "#统计出现次数\n",
    "num_a=0\n",
    "num_b=0\n",
    "\n",
    "#遍历list lst1    遍历每一个元素\n",
    "for i in lst1:\n",
    "    if i==\"a\":\n",
    "        num_a+=1\n",
    "    elif i==\"b\":\n",
    "        num_b+=1\n",
    "    else:\n",
    "        print(\"未统计\")\n",
    "\n",
    "print(\"num_a=\",num_a)\n",
    "print(\"num_b=\",num_b)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "#根据给定次数循环\n",
    "#range(n)=[0,n) 前闭后开\n",
    "for i in range(5):\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "#while语句类似 前面已经写过一个demo了\n",
    "i=0\n",
    "while i <len(range(5)):\n",
    "    print(i)\n",
    "    i+=1    #设置循环变量的变化条件，不然会陷入死循环"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "循环嵌套 类似 不再赘述"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找了3次，终于找到了！\n",
      "总的查询次数:3次\n"
     ]
    }
   ],
   "source": [
    "#break和continue\n",
    "#break用于提前跳出一层循环\n",
    "#continue用于执行下一次循环\n",
    "\n",
    "lst43=[\"悟空\",\"八戒\",\"白骨精\",\"唐僧\",\"沙僧\",\"牛魔王\"]\n",
    "#变量用来记录找的次数或循环次数\n",
    "i=1\n",
    "for item in lst43:\n",
    "    #与列表中每个元素进行匹配，一旦匹配上，打印已找到，然后退出循环。\n",
    "    if item==\"白骨精\":\n",
    "        print(\"找了%d次，终于找到了！\"%(i))\n",
    "        break\n",
    "    else:\n",
    "        i=i+1\n",
    "print(\"总的查询次数:%d次\"%i)   #因为找到以后我们就直接推出了"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找了3次，终于找到了！\n",
      "总的查询次数:6次\n"
     ]
    }
   ],
   "source": [
    "lst43=[\"悟空\",\"八戒\",\"白骨精\",\"唐僧\",\"沙僧\",\"牛魔王\"]\n",
    "#变量用来记录找的次数或循环次数\n",
    "i=1\n",
    "for item in lst43:\n",
    "    #与列表中每个元素进行匹配，一旦匹配上，打印已找到，然后退出循环。\n",
    "    if item==\"白骨精\":\n",
    "        print(\"找了%d次，终于找到了！\"%(i))\n",
    "        continue\n",
    "    else:\n",
    "        i=i+1\n",
    "print(\"总的查询次数:%d次\"%i)   #找到后还在继续遍历列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}